package com.fstec.ks.master;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;

import com.sinotn.SinotnLogger;
import com.sinotn.gson.GsonFacade;

public abstract class TestDataBuilder {
	
	public static <T> T fromFile(File file, Type type){
		String json=readText(file);
		return GsonFacade.fromJson(json, type);
	}

	private static String readText(File file){
		BufferedReader br=null;
		try{
			StringBuilder sb=new StringBuilder();
			br=new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
			for(String line=br.readLine();line!=null;line=br.readLine()){
				line=line.trim();
				if(line.length()==0||line.startsWith("//")) continue;
				sb.append(line);
			}
			return sb.toString();
		}catch(Throwable e){
			SinotnLogger.DEBUG.error("读文件文本错误"+file, e);
		}finally{
			if(br!=null){
				try{
					br.close();
				}catch(Throwable e){}
			}
		}
		return "{}";
	}
}
